/**
 * // TODO generated, please specify type and doc for the params
 * @param {Object} frm
 *
 * @properties={typeid:24,uuid:"ADD9FDE1-4D04-4531-96D7-850F8BCA2BB0"}
 */
function doEdit(frm) {

	if (!globals.isEditing()) globals.startEditing()
	var allNames = forms[frm].elements.allnames
	for (var i = 0; i < allNames.length; i++) {
		if (allNames[i].indexOf('fld_') >= 0) {
			forms[frm].elements[allNames[i]].bgcolor = '#feffe4';
			forms[frm].elements[allNames[i]]["readOnly"] = false;
		}
		if (allNames[i].indexOf('vlst_') >= 0) {
			forms[frm].elements[allNames[i]].bgcolor = '#feffe4';
			forms[frm].elements[allNames[i]]["readOnly"] = false;
		}
	}
	
	forms[frm].controller.focusFirstField();
}

/**
 * @param {String} frm
 *
 * @properties={typeid:24,uuid:"6893A501-C5B8-4610-9361-6319EC59EB14"}
 */
function hide_btn_reset_fields(frm) {

	if (globals.isEditing()) globals.cancelEditing();
	var allNames = forms[frm].elements.allnames
	for (var i = 0; i < allNames.length; i++) {
		if (allNames[i].indexOf('fld_') >= 0) {
			forms[frm].elements[allNames[i]]["readOnly"] = true;
			forms[frm].elements[allNames[i]]["bgcolor"] = '#F5F7F9';
		}
		if (allNames[i].indexOf('vlst_') >= 0) {
			forms[frm].elements[allNames[i]].bgcolor = '#F5F7F9';
			forms[frm].elements[allNames[i]]["readOnly"] = true;
		}
	}
}

/**
 * @properties={typeid:24,uuid:"3FD1AD11-A9AF-4630-8C7B-F175450A5CFE"}
 */
function cancelEditing() {
	
	databaseManager.revertEditedRecords();
	databaseManager.setAutoSave(true);
}

/**
 * @properties={typeid:24,uuid:"A2F9CFFD-F2C2-485F-975A-3321415BA422"}
 */
function isEditing() {
	return !databaseManager.getAutoSave();
}

/**
 * @properties={typeid:24,uuid:"B5CA1209-3443-4F55-8783-70840F310F57"}
 */
function startEditing() {
	databaseManager.setAutoSave(false);
}

/**
 * @param {JSFoundSet} fs
 *
 * @properties={typeid:24,uuid:"95BEDD80-6286-494A-8D40-2A894ABC0433"}
 */
function saveFoundset(fs) {
	
	var success = databaseManager.saveData(fs);
	
	if (success == false){
		
		var texto_error = "";
		var array = databaseManager.getFailedRecords(fs)
		for( var i = 0 ; i < array.length ; i++ )
		{
			var record = array[i];
			texto_error += record.exception.getMessage() + "\n";
		}
		
		plugins.dialogs.showErrorDialog("",texto_error);
		return false;
	}
	
	return true;
}

/**
 * @param {JSFoundSet} fs
 *
 * @properties={typeid:24,uuid:"071CD29C-3F87-4B0D-A8AB-148B16845D3B"}
 */
function cancel_record(fs) {

	databaseManager.revertEditedRecords(fs);
}

/**
 * @param {JSFoundSet} fs
 *
 * @properties={typeid:24,uuid:"4800EE34-3695-4E5C-9BAF-7F882C9DA1C0"}
 */
function deleteFoundset(fs){

	var vbok = fs.deleteAllRecords();
	return vbok;
}

/**
 * @param {JSFoundSet} fs
 * @param {JSRecord} jsrecord
 *
 * @properties={typeid:24,uuid:"B6AEB34E-D9F7-444B-8A23-3A2F1842FCD5"}
 */
function deleteRecord(fs, jsrecord) {

	var bok = plugins.dialogs.showWarningDialog("Borrar Ficha","¿Está seguro de querer borra la Ficha?","Aceptar", "Cancelar");
	if (bok == "Aceptar"){
		var vbokDelete = fs.deleteRecord(jsrecord);
		return vbokDelete;
	}
	return false;
}
